//
//  Soldier.m
//  TowerDefence
//
//  Created by K3 on 6/4/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "SoldierFromBarrack.h"
#import "GameLayer.h"
#import "PlayScreen.h"
#import "iCoreGUIController.h"

@implementation SoldierFromBarrack
-(id)initBarrackSoldierWith:(GameLayer*)_gameLayer withBarrack:(BarrackTower*)_barrackTower{
    self=[super initSoldierWith:_gameLayer];
    HP=110;
    armor=50;
    speed=1.5f;
    damage=5;
    inBattle=NO;
    lockTarget=NO;
    attackRange=30.0f;
    barrackTower=_barrackTower;
    standingPosition=ccp(0,0);
    [barrackTower.soldiers addObject:self];
    spriteBody=[[CCSprite alloc]initWithSpriteFrameName:@"Barrack LV1/Troop_Move_01.png"];
    [self drawBloodMeterWithLength:spriteBody.contentSize.width/2 + 10 width:3 position:ccp(spriteBody.contentSize.width/2, spriteBody.contentSize.height)];
    return self;
}
-(void)update:(ccTime)_timeUpdate{
    [super update:_timeUpdate];
    if(HP<=0)
        [barrackTower.soldiers removeObject:self];
}

-(void)animationBattle {    
    inBattle = YES;
    [spriteBody stopAllActions];
    [super animationBattle];
    CCAnimation *animation = [self loadAnimationFromCacheByName:@"Barrack LV1/Troop_Attack" withCount:4 delay:0.08];
    
    CCCallFunc *func=[CCCallFunc actionWithTarget:self selector:@selector(battle)];
    [spriteBody runAction:[CCRepeatForever actionWithAction:[CCSequence actions:[CCAnimate actionWithAnimation:animation restoreOriginalFrame:YES], func,[CCDelayTime actionWithDuration:0.5f], nil]]];
}

-(void)animationMoveLeft{
    [super animationMoveLeft];
    CCAnimation *animation = [self loadAnimationFromCacheByName:@"Barrack LV1/Troop_Move" withCount:5 delay:0.08];
    [spriteBody runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation restoreOriginalFrame:YES]]];
}

-(void)animationDie{
    [super animationDie];
    CCAnimation *animation = [self loadAnimationFromCacheByName:@"Barrack LV1/Troop_Die" withCount:6 delay:0.08];
    CCCallFunc *dieCallBack = [CCCallFunc actionWithTarget:self selector:@selector(dieCallBack)];
    [spriteBody runAction:[CCSequence actions:[CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO],[CCDelayTime actionWithDuration:0.75f], dieCallBack, nil]];
}

-(void)dealloc {
    NSLog(@"Goblin dealoc");
    [self removeAnimationInFrameCache:@"Barrack LV1/Troop_Attack"];
    [self removeAnimationInFrameCache:@"Barrack LV1/Troop_Move"];
    [self removeAnimationInFrameCache:@"Barrack LV1/Troop_Die"];
    [super dealloc];
}

@end
